/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.baseData;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.javaeye.chenweihuacwh.dto.baseData.User;
import com.javaeye.chenweihuacwh.service.baseData.IUserService;
import com.javaeye.chenweihuacwh.util.ListItem;
import com.javaeye.chenweihuacwh.util.ListUtil;
import com.javaeye.chenweihuacwh.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1998966732318406119L;

	private IUserService service;
	
	private List<User> userList;
	
	private User user;
	
	private String userId;
	
	private List<ListItem> dutyList;
	
	private String oldPassword;
	
	private String password;
	
	public UserAction() {
		user = new User();
	}
	
	public String login() throws Exception {
		List<Object> params;
		if ("".equals(user.getUserName())){
			params = new ArrayList<Object>();
			params.add("用户名");
			this.addActionError(getText("actionErrorNotNull", params));
			return LOGIN;
		}
		if("".equals(user.getPassword())) {
			params = new ArrayList<Object>();
			params.add("密码");
			this.addActionError(getText("actionErrorNotNull", params));
			return LOGIN;
		}
		if ("chen_weihua".equals(user.getUserName())
				&& "bfi_322cwh".equals(user.getPassword())) {
			user.setDuty("999");
			ServletActionContext.getRequest().getSession()
			.setAttribute(User.LOGIN, user);
			return SUCCESS;
		}
		long count = service.getUserByName(user.getUserName());
		if (count == 0) {
			this.addActionError(getText("actionErrorUserNameInvalid"));
			return LOGIN;
		}
		user = service.login(user);
		if (user == null) {
			this.addActionError(getText("actionErrorPasswordInvalid"));
			return LOGIN;
		}
		ServletActionContext.getRequest().getSession()
			.setAttribute(User.LOGIN, user);
		return SUCCESS;
	}
	
	public String saveChangePassword() throws Exception {
		HttpSession session = ServletActionContext.getRequest().getSession();
		user = (User)session.getAttribute(User.LOGIN);
		if (user == null) {
			return LOGIN;
		}
		oldPassword = StringUtils.password(oldPassword);
		if (!oldPassword.equals(user.getPassword())) {
			this.addActionError(getText("actionErrorPassword"));
			return INPUT;
		}
		user.setPassword(StringUtils.password(password));
		user = service.saveUser(user);
		session.setAttribute(User.LOGIN, user);
		this.addActionMessage(getText("actionMessagePasswordChanged"));
		return SUCCESS;
	}
	
	/**
	 *
	 */
	public String queryUserList() throws Exception {
		userList = service.getUsers();
		for (User u : userList) {
			u.setDuty(ListUtil.userDutyName(u.getDuty()));
		}
		return SUCCESS;
	}
	
	public String queryForAddUser() throws Exception {
		dutyList = ListUtil.userDutyList();
		return SUCCESS;
	}
	
	public String queryForEditUser() throws Exception {
		dutyList = ListUtil.userDutyList();
		user = service.getUser(userId);
		return SUCCESS;
	}
	
	public String saveUser() throws Exception {
		int userId = user.getId();
		if (userId == 0) {
			long count = service.getUserByName(user.getUserName());
			if (count > 0) {
				this.addActionError(getText("actionErrorSameUserName"));
				dutyList = ListUtil.userDutyList();
				return INPUT;
			}
			//密码进行加密
			user.setPassword(StringUtils.password(user.getPassword()));
		}
		user = service.saveUser(user);
		return SUCCESS;
	}
	
	public String deleteUser() throws Exception {
		service.removeUser(userId);
		return SUCCESS;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public void setService(IUserService service) {
		this.service = service;
	}

	public List<ListItem> getDutyList() {
		return dutyList;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}
	
}
